//
//  TF_BackQuestionCell.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/12/28.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit


class TF_BackQuestionCell: UITableViewCell {
    @objc public var cellwidth:CGFloat = 0
    var nameLab:UILabel!
    var avater:UIImageView!
    var replyView:UIView! //回复
    var isOrientationLandscape:Bool = false //YES是横屏;NO为竖屏
    var isChatArea:Bool = false    //区分提问记录和互动区域
    var isQuestion:Bool = false
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        
        self.replyView = UIView.init()
        self.replyView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.8)
        self.replyView.layer.cornerRadius = 14;
        self.replyView.layer.masksToBounds = true;
        self.contentView.addSubview(self.replyView)
        
    }
    @objc public func setmodel(model:TFPlaybackQuestionData) {
        let nickNameColor:UIColor
        let msgColor:UIColor
        let replymsgColor:UIColor
        let replyNicknameColor:UIColor
    
        
        if (self.isOrientationLandscape==false) { //竖屏
            self.replyView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.8)
            
            if (model.answer.count > 0 ) { // 回复
                nickNameColor = UIColor(red: 168/255.0, green: 178/255.0, blue: 188/255.0, alpha: 0.6)
           
                msgColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 0.5)
                replyNicknameColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1.0)
            } else {
                nickNameColor =  UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1.0)
                msgColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
                replyNicknameColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1.0)
            }
              replymsgColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
        } else {
            if (self.isChatArea) {
                self.replyView.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)
                msgColor = UIColor.white
                nickNameColor = UIColor(red: 65/255.0, green: 130/255.0, blue: 235/255.0, alpha: 1.0)
                replymsgColor = UIColor.white
                replyNicknameColor =  UIColor(red: 65/255.0, green: 130/255.0, blue: 235/255.0, alpha: 1.0)
            } else {
                self.replyView.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1.0)
                if (model.answer.count > 0) {
                    msgColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 0.5)
                } else {
                    msgColor =  UIColor(red: 24/255.0, green: 24/255.0, blue: 24/255.0, alpha: 1.0)
                }
               
                nickNameColor = UIColor(red: 168/255.0, green: 178/255.0, blue: 188/255.0, alpha: 0.6)
                replymsgColor = UIColor(red: 24/255.0, green: 24/255.0, blue: 24/255.0, alpha: 1.0)
                replyNicknameColor = UIColor(red: 150/255.0, green: 160/255.0, blue: 170/255.0, alpha: 1.0)
            }
           
           
        }
        
        self.replyView.subviews.forEach { subview in
            subview.removeFromSuperview()
        }
        self.nameLab = UILabel.init()
   
        self.nameLab.font = UIFont.PingFang(size: 14)
        self.nameLab.numberOfLines = 0;
        self.nameLab.lineBreakMode = .byCharWrapping
        self.replyView.addSubview(self.nameLab)
        
        self.avater = UIImageView.init()
        self.avater.contentMode = .scaleAspectFit
        self.replyView.addSubview(self.avater)
      
        if (self.isQuestion) {
            self.avater.sd_setImage(with: NSURL.init(string: model.avatar) as URL?, completed: nil)
        } else {
            self.avater.image = UIImage.init(named: "HTLive.bundle/chat_tag_ask")
     

        }
        let nickname = model.nickname + " "
        let str = nickname + model.content
        let AttributedString = NSMutableAttributedString.init(string: str)
        if (model.answer.count > 0) {
            let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: msgColor, ]
            AttributedString.addAttributes(attr, range: NSRange(location: 0, length: AttributedString.length))
            let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: nickNameColor, ]
            AttributedString.addAttributes(attr1, range: NSRange(location: 0, length: model.nickname.count))
        
        } else {
            
            let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: msgColor, ]
            AttributedString.addAttributes(attr, range: NSRange(location: 0, length: AttributedString.length))
            let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: nickNameColor, ]
            AttributedString.addAttributes(attr1, range: NSRange(location: 0, length: model.nickname.count))
        }
        let font:UIFont = UIFont.systemFont(ofSize: 14)
        let size = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.cellwidth-35-16-12-16, labelfont: font)
        
        
        
        self.avater.frame = CGRect(x: 6, y: 6, width: 20, height: 20)
        self.nameLab.frame = CGRect(x: self.avater.frame.maxX+8, y: 6, width: size.width, height: size.height)
    
        self.nameLab.attributedText = AttributedString;
        
        if (model.answer.count > 0) {
            var height:CGFloat = 0;
            var anserwidth:CGFloat = 0;
          
            for i in 0..<model.answer.count {
                let answer:TFPlaybackQuestionAnswer = model.answer[i] as! TFPlaybackQuestionAnswer
                let img = UIImageView.init()
                if self.isQuestion {
                    img.sd_setImage(with: NSURL.init(string: answer.avatar) as URL?, completed: nil)
                } else {
                    img.image = UIImage.init(named: "HTLive.bundle/chat_tag_answer")
                }
                
                let lab = UILabel.init()
                lab.numberOfLines = 0
                lab.lineBreakMode = .byCharWrapping
                
                let line = UILabel.init()
                if self.isOrientationLandscape {
                    
                    line.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 248/255.0, alpha: 0.2)
                } else {
                    line.backgroundColor = UIColor(red: 244/255.0, green: 244/255.0, blue: 248/255.0, alpha: 1.0)
                }
                self.replyView.addSubview(line)
                self.replyView.addSubview(img)
                self.replyView.addSubview(lab)
         
                let str1 = answer.nickname + " " + answer.content
                let AttributedString1 = NSMutableAttributedString.init(string: str1)

                let attr132: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: replyNicknameColor,]
                AttributedString1.addAttributes(attr132, range: NSRange(location: 0, length: AttributedString1.length))
             
                let attch = NSTextAttachment.init()
                if answer.role == .roleAdmin { //助教
                    attch.image = UIImage.init(named: "HTLive.bundle/chat_tag_assistant")
                } else if (answer.role == .roleSpadmin) {//老师
                    attch.image = UIImage.init(named: "HTLive.bundle/chat_tag_teacher")
                }
               
                attch.bounds = CGRect(x: 0, y: -3, width: 27, height: 15)
                let imageStr = NSAttributedString.init(attachment: attch)
                AttributedString1.insert(imageStr, at: answer.nickname.count+1)
                let Attri = NSAttributedString.init(string: " ")
                AttributedString1.insert(Attri, at: answer.nickname.count+2)
                let AttributeName: [NSAttributedString.Key : Any] = [.kern:5]
                AttributedString1.addAttributes(AttributeName, range: NSRange(location: answer.nickname.count+2, length: 1))
                
                
                let attr22: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: replymsgColor, ]
                
                AttributedString1.addAttributes(attr22, range: NSRange(location: answer.nickname.count+2, length: answer.content.count+1))
                let attr24: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),]
                AttributedString1.addAttributes(attr24, range: NSRange(location: 0, length: AttributedString1.length))
          
                let answersize = TalkfunSharedTools.getSizeLabel(labelString: AttributedString1, labelWidth:self.cellwidth-35-16-12-16, labelfont: font)
                
               //取最大值
               
                anserwidth = answersize.width > anserwidth ? answersize.width:anserwidth
                line.frame = CGRect(x: 6, y: 10+size.height+height, width: anserwidth+28, height: 1)
                img.frame = CGRect(x: 6, y: line.frame.maxY+4, width: 20, height: 20)
                lab.frame = CGRect(x: img.frame.maxX+8, y: line.frame.maxY+4, width: answersize.width, height: answersize.height)
             
                height = height+answersize.height+10;
                lab.attributedText = AttributedString1;
                
            }
           
             let k = size.width > anserwidth ? size.width:anserwidth
        
            if (self.isOrientationLandscape) {
                self.replyView.frame = CGRect(x: 12, y: 4, width: k+16+35, height: size.height+10+1+height)
               
            } else {
                self.replyView.frame = CGRect(x: 12, y: 8, width: k+16+35, height: size.height+10+1+height)
             
            }
            self.replyView.addSubview(self.avater)
            self.replyView.addSubview(self.nameLab)

        } else {
            
            if (self.isOrientationLandscape) {
                self.replyView.frame = CGRect(x: 12, y: 4, width: size.width+16+35, height: size.height+12)
            } else {
                self.replyView.frame = CGRect(x: 12, y: 8, width: size.width+16+35, height: size.height+12)
               
            }
        
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
